package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         325,
		Discription: "和为k的最长子数组",
		Level:       2,
		Labels: map[string]int{
			"前缀和": 1,
			"哈希表": 1,
		},
	})
}

func MaxSubArrayLen(nums []int, k int) int {
	preSumMap := make(map[int]int, 0)
	//表示不需要减去任何前缀
	preSumMap[0] = -1
	preSum := 0
	res := 0
	for i := range nums {
		preSum += nums[i]
		if _, exist := preSumMap[preSum]; !exist {
			preSumMap[preSum] = i
		}

		if index, exist := preSumMap[preSum-k]; exist {
			res = max(res, i-index)
		}
	}

	return res
}